const app = getApp()
const db = wx.cloud.database()
const _ = db.command

Page({  
  data: {
      avatarUrl: './user-unlogin.png',
      userInfo: {},
      mine: {
        userName: '未登录'
      },
      name: "",
      minePull: {},
      mineBuyCar: {},
    },
  
    onLoad: function() {
      // 获取用户信息
      this.getUserInfo()
      this.getDBData()
    },

    getUserInfo(){
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            wx.getUserInfo({
              success: res => {
                this.setData({
                  avatarUrl: res.userInfo.avatarUrl,
                  userInfo: res.userInfo,
                  name: res.userInfo.nickName
                })
              }
            })
          }
        }
      })      
    },

    //获取数据库信息
    getDBData(){
      app.appGetOpenId().then(res=>{
        db.collection('mine').where({
          _openid: res,
        }).get().then(res=>{
          var that = this
          var mine = (res.data)[0]
          that.setData({mine: mine});
          that.setData({minePull: mine.minePull});
          that.setData({mineBuyCar: mine.mineBuyCar});
          //console.log(mine.likes)
        })
      })
    },
      
    //个人页面跳转
    toMyInfo: function(option){
      //获取点击事件的id
      var curId = option.currentTarget.id;
      switch(curId){
        case "0":    
          wx.navigateTo({
            url: "../../pages/myPull/myPull"
          })
        case "1":    
          wx.navigateTo({
            url: "../../pages/buyCar/buyCar"
          })
        break;
      }
    }
})